package com.lxy.thread;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.lxy.util.MyCityData;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public class AllThread {
	private String url_Thread_PM2_5;
	private String url_Thread_realTime;
	private String url_Thread_SixDay;
	
	private Thread_PM2_5 myPM;
	private Thread_SixDay mySixday;
	private Thread_realTime myReal;
	
	private Context c;
	private boolean flag=false;
	
	public AllThread(Handler h,Context c,String city){
		this.c=c;
		MyCityData myc=new MyCityData(c);
		String s_city=myc.getCitys(city);
		
		try {
			url_Thread_PM2_5="http://www.pm25.in/api/querys/pm2_5.json?city="+URLEncoder.encode(city,"utf-8")+"&token=5j1znBVAsnSf5xQyNQyq";
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		url_Thread_realTime="http://www.weather.com.cn/data/sk/"+s_city+".html";
		url_Thread_SixDay="http://m.weather.com.cn/data/"+s_city+".html";
		
		Log.i("url_Thread_PM2_5-->",url_Thread_PM2_5);
		
		myPM=new Thread_PM2_5(url_Thread_PM2_5, h);
		myReal=new Thread_realTime(url_Thread_realTime, h);
		mySixday=new Thread_SixDay(url_Thread_SixDay, h);
	}
	
	public void start(){
		if(isOnline()){
			myPM.start(); //pass
			myReal.start();//pass
			mySixday.start();
		}
	}
	
	public void interrupt(){
		if(flag){
			myPM.interrupt();
			myReal.interrupt();
			mySixday.interrupt();
		}
	}
	
	private boolean isOnline(){
		ConnectivityManager cwjManager=(ConnectivityManager)c.getSystemService(Context.CONNECTIVITY_SERVICE); 
		NetworkInfo info = cwjManager.getActiveNetworkInfo(); 
		if (info != null && info.isAvailable()){ 
			flag=true;
			Log.i("net-->","success");
		} 
		else{
			Toast.makeText(c,"无互联网连接",Toast.LENGTH_SHORT).show();
			flag=false;
			Log.i("net-->","failed");
		}
		return flag;
	}
	
}
